import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class CustomEditDialog extends Dialog {
  final String title;
  var content;
  int maxLength;
  int type = 1;
  List<String> allListName = [];

  TextEditingController editingController;

  CustomEditDialog(this.title, this.content, {maxLength = 5, this.type,this.allListName}) {
    editingController = TextEditingController(
        text: content == null || (content is int && content < 0)
            ? ""
            : content.toString());
  }

  @override
  Widget build(BuildContext context) {
    return //Material( child:  Scaffold(  resizeToAvoidBottomInset: true,  body: Center( child:
        Container(
            color: Colors.white,
            height: 180,
            width: 260,
            child: Column(children: <Widget>[
              Padding(
                  padding: EdgeInsets.all(12),
                  child: Stack(children: <Widget>[
                    Align(
                        child: Text(title, style: TextStyle(fontSize: 17)),
                        alignment: Alignment.topCenter),
                  ])),
              Divider(height: 1),
              Container(
                  padding: EdgeInsets.all(2),
                  height: 62,
                  alignment: Alignment.center,
                  width: double.infinity,
                  child: getTextField(content)),
              Divider(),
              Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                        child: GestureDetector(
                            behavior: HitTestBehavior.translucent,
                            onTap: () {
                              Navigator.pop(context);
                            },
                            child: Container(
                                alignment: Alignment.center,
                                padding: EdgeInsets.only(
                                    left: 28, top: 2, bottom: 2, right: 28),
                                child: Text("取消",
                                    style: TextStyle(fontSize: 17)))),
                        flex: 1),
                    Container(
                        margin: EdgeInsets.only(top: 6),
                        color: Color(0xffdddddd),
                        width: 1,
                        height: 28),
                    Expanded(
                        child: GestureDetector(
                            behavior: HitTestBehavior.translucent,
                            onTap: () {
                              String str = editingController.text;
                              if (null != str && "" != str) {
                                if (str.length > 6) {
                                  EasyLoading.showInfo("相册名超过长度限制");
                                } else if (type == 1 && str.replaceAll(" ", "") == "") {
                                    EasyLoading.showInfo("相册名不可以为空白");
                                } else if (type == 1 && allListName.isNotEmpty && allListName.contains(str)) {
                                  EasyLoading.showInfo("该相册名已存在");
                                } else if (type != 1 && int.parse(str) < 50) {
                                  EasyLoading.showInfo("范围太小，请重新设置");
                                } else {
                                  Future.delayed(Duration(milliseconds: 300),
                                      () {
                                    print(str);
                                    Navigator.pop(context, str);
                                    // Navigator.of(buildContext).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => BottomNavigation()), (route) => false);
                                  });
                                }
                              } else {
                                EasyLoading.showInfo(
                                    type == 1 ? "相册名不可为空" : "请填写范围");
                              }
                            },
                            child: Container(
                                alignment: Alignment.center,
                                padding: EdgeInsets.only(
                                    left: 28, top: 2, bottom: 2, right: 28),
                                child: Text("确定",
                                    style: TextStyle(fontSize: 17)))),
                        flex: 1)
                  ])
            ]));
    // decoration: BoxDecoration(
    //     color: Colors.white,
    //     borderRadius: BorderRadius.all(Radius.circular(28))));
    // )), type: MaterialType.transparency);
  }

  TextField getTextField(var content) {
    if (content is int) {
      return TextField(
          controller: editingController,
          textAlign: TextAlign.center,
          maxLength: maxLength,
          autofocus: true,
          inputFormatters: [
            LengthLimitingTextInputFormatter(5),
            FilteringTextInputFormatter.digitsOnly
          ],
          keyboardType: TextInputType.number,
          decoration: InputDecoration(border: InputBorder.none));
    } else {
      return TextField(
          controller: editingController,
          maxLength: 6,
          inputFormatters: [LengthLimitingTextInputFormatter(6)],
          autofocus: true,
          textAlign: TextAlign.center,
          decoration: InputDecoration(border: InputBorder.none));
    }
  }
}
